<?php
declare(strict_types=1);

namespace App\Support;

use App\Constants\ErrorCode;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Utils\Context;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;


class Response
{
    /**
     * @Inject
     * @var ResponseInterface
     */
    private $response;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;


    /**
     * 处理json数据
     *
     * @param $data
     * @return PsrResponseInterface
     */
    public function json($data): PsrResponseInterface
    {
        return $this->response->json($data);
    }


    /**
     * dating 项目成功返回
     * @param array $data
     * @param int $code
     * @return PsrResponseInterface
     */
    public function returnSuccess(array $data = [], int $code = ErrorCode::SUCCESS): PsrResponseInterface
    {
        $message = trans("messages." . $code);
        $resp = [
            "code" => $code,
            "message" => $message,
        ];

        //统一返回驼峰命名
        if ($data) {
            $resp["data"] = transformCamelArray($data);
        }


        $this->addResponse($resp);


        return $this->response->json($resp);
    }


    /**
     * dating 项目错误返回
     * @param int $code
     * @param string $error 有指定错误才返回 系统错误 或者 抛出的错误
     * @return PsrResponseInterface
     */
    public function returnFail(int $code = ErrorCode::ERROR, string $error = ''): PsrResponseInterface
    {
        $message = trans("messages." . $code);
        $resp = [
            "code" => $code,
            "message" => $message,
        ];
        if (!empty($error)) {
            $resp['error'] = $error;
        }
        $this->addResponse($resp);
        return $this->response->json($resp);
    }

    /**
     * dating 项目后台返回
     * @param int $code
     * @param string $error 有指定错误才返回 系统错误 或者 抛出的错误
     * @return PsrResponseInterface
     */
    public function returnAdminJson(array $data = [], int $code = ErrorCode::SUCCESS, string $message = ''): PsrResponseInterface
    {
        $message = $message ? $message : trans("messages." . $code);
        $resp = [
            "code" => $code,
            "message" => $message,
            "data" => transformCamelArray($data)
        ];
        $this->addResponse($resp);
        return $this->response->json($resp);
    }


    /**
     * 后台错误通用返回
     * @param $message
     * @param $code
     * @return PsrResponseInterface
     */
    public function adminFail($message='错误',$code = 101){
        $resp = [
            "code" => $code,
            "message" => $message,
            "data" => []
        ];

        return $this->response->json($resp);
    }

    /**
     * 后台成功返回
     * @param $data
     * @param $message
     * @return PsrResponseInterface
     */
    public function adminSuccess($data = [],$message = '操作成功'){
        $resp = [
            "code" => 200,
            "message" => $message,
            "data" => transformCamelArray($data)
        ];
        return $this->response->json($resp);
    }



    public function addResponse($data)
    {
        $rest = $this->request->getAttribute(Dispatched::class)->handler;

        try {
            $requestId = Context::get('requestId');
            $data = [
                'route' => $rest->route,
                'body' => json_encode($data),
                'request_id' => $requestId,
                'time'=>time()
            ];

//            $requestId = \App\Model\Response::addResponseLog($data);

        } catch (\Exception $e) {

        }
    }


}
